package services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParamBean;
import org.apache.http.protocol.HTTP;
import android.util.Log;
import settings.AppSettings;
import user.User;

public class ServerAccount 
{
    public static boolean create(User user)
    {
        boolean status = false;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        try
        {
            StringBuilder createUrl = new StringBuilder(AppSettings.AppUrl).
                                                append(keywords.Servlets.Register).
                                                append("mode=doregister");
            
            HttpPost httpPost = new HttpPost(createUrl.toString());
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair(keywords.Params.firstName, user.getFirstName()));
            nvps.add(new BasicNameValuePair(keywords.Params.lastName, user.getLastName()));
            nvps.add(new BasicNameValuePair(keywords.Params.emailAddress, user.getEmail()));
            nvps.add(new BasicNameValuePair(keywords.Params.userName, user.getUserName()));
            nvps.add(new BasicNameValuePair(keywords.Params.password, user.getPassword()));
            nvps.add(new BasicNameValuePair(keywords.Params.password, user.getPassword())); // Password x 2 - like in the GUI interface.
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));   	
            HttpResponse response = httpClient.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            status = (statusCode == HttpStatus.SC_OK);
        }
        catch(ClientProtocolException protocolException)
        {
            Log.e(AppSettings.DebugPrefix, protocolException.toString());
        }
        catch(IOException ioException)
        {
            Log.e(AppSettings.DebugPrefix, ioException.toString());
        }
        finally
        {
            httpClient.getConnectionManager().shutdown();
        }
        return status;
    }
    
    public static boolean update(User user)
    {
        boolean success = false;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        try
        {
            StringBuilder updateUrl = new StringBuilder(AppSettings.AppUrl).
                                            append(keywords.Servlets.MyProfile).
                                            append("mode=dosave");
            
            HttpPost httpPost = new HttpPost(updateUrl.toString());
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair(keywords.Params.firstName, user.getFirstName()));
            nvps.add(new BasicNameValuePair(keywords.Params.lastName, user.getLastName()));
            nvps.add(new BasicNameValuePair(keywords.Params.emailAddress, user.getEmail()));
            nvps.add(new BasicNameValuePair(keywords.Params.userName, user.getUserName()));
            nvps.add(new BasicNameValuePair(keywords.Params.password, user.getPassword()));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
            
            HttpParams params = new BasicHttpParams();
            HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
            paramsBean.setUserAgent(AppSettings.httpClientUserAgent);
            httpPost.setParams(params);
            
            HttpResponse response = httpClient.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            success = (statusCode == HttpStatus.SC_OK);
        }
        catch(ClientProtocolException protocolException)
        {
            Log.e(AppSettings.DebugPrefix, protocolException.toString());
        }
        catch(IOException ioException)
        {
            Log.e(AppSettings.DebugPrefix, ioException.toString());
        }
        finally
        {
            httpClient.getConnectionManager().shutdown();
        }
        return success;    			
    }
}
